<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>             
<%@ page import="java.net.URLEncoder"%>
<%@ page import="java.security.PublicKey"%>
<%@ page import="java.security.PrivateKey"%>
<%@ page import="com.icbc.ssic.base.Credentials"%>
<%@ page import="com.icbc.ssic.base.SSICUser"%>
<%@ page import="com.icbc.ssic.base.UpdateTellerSiteClient"%>
<%@page import="com.icbc.ctpdemo.uniform_attestation.SsicManager"%>
<%@page import="com.icbc.ctp.util.MessageInstance"%>
<%@ page import="java.net.URLEncoder"%>
<%@ include file="/uniform_attestation/head_beforesignon.jsp" %>    
<%@ taglib uri="ctp-tag" prefix="ctp" %>
<%
	String SSIAuth=request.getParameter("SSIAuth");
	String SSISign=request.getParameter("SSISign");
	String sessionId = request.getParameter("dse_sessionId");
	String siteOperationName="signInForce";
	String style="1";
	String userName="init";
	String userId = "";
	String mode ="3";
	try{
	userId=(String)((ChannelContext)request.getAttribute(IChannel.CHANNEL_CONTEXT)).getValueAt("sessCtx.user.id");
	mode = (String)request.getParameter("mode");
	}catch(Exception e){
		userId=(String)request.getParameter("userId");
	}
%>

<%@page import="com.icbc.ctp.core.impl.Context"%>
<%@page import="com.icbc.ctp.channel.ChannelContext"%>
<%@page import="com.icbc.ctp.channel.IChannel"%>

<%@page import="com.icbc.ctp.core.IContext"%><jsp:useBean id="logonsign" scope="page" class="com.icbc.ssic.base.ServerSideAuthenticator">
<% 
	String serverIP = SsicManager.getInstance().getSsicIp();
	String clientIP = SsicManager.getInstance().getClientIp();	  
	String Language = "";
	try{ 		
		Language=(String)((ChannelContext)request.getAttribute(IChannel.CHANNEL_CONTEXT)).getValueAt("sessCtx.Language");	
	}catch(Exception e){
	}	
	String siteURL= "http://" + clientIP + path + "/uniform_attestation/authorization.jsp?dse_sessionId="+sessionId+"&Language="+Language+"&userId="+userId+"&mode="+mode;
	String siteVersion = SsicManager.getInstance().getSsicVersion();
	String siteService = SsicManager.getInstance().getSsicClientKeyName();
	PublicKey publicKey = SsicManager.getInstance().getPublicKey();
	PrivateKey privateKey = SsicManager.getInstance().getPrivateKey();

	logonsign.setServerName(serverIP);
	logonsign.setVersion(siteVersion);
	logonsign.setServiceName(siteService);
	logonsign.setServiceURL(siteURL);
	logonsign.setOperation(siteOperationName);
	logonsign.setSSIPublicKey(publicKey);
	logonsign.setPrivateKey(privateKey);
	logonsign.setStyle(style);
	logonsign.setErrorPageName("/uniform_attestation/logonerror.jsp");
	
       if(!logonsign.execute(request,response,SSIAuth,SSISign))
		return;
%> 
</jsp:useBean>

<%	
    String flag=null;int flagauth=0;//双人授权标志,1表示失败,2表示成功
	String locale=(String)request.getParameter("Language");
	String authormsg001 = null,authormsg002 = null,authormsg003=null;
		try{
				authormsg001 = MessageInstance.getSingleton().getMessage("ssic","authormsg001",locale);	
				authormsg002 = MessageInstance.getSingleton().getMessage("ssic","authormsg002",locale);	
				authormsg003 = MessageInstance.getSingleton().getMessage("ssic","authormsg003",locale);	
		}catch(Exception e){		
		}
	String target=request.getParameter("target");
	String actionName1=request.getParameter("flowActionName");
	String flowName1=request.getParameter("flowName");
	Credentials cred=null;
	SSICUser user=null;
	Object obj=null;
	obj=request.getAttribute("ssiCredentials");
	String errorMsg ="";
	
	if(obj!=null)
	{
		cred=(Credentials)obj;
		user=cred.getSSICUser();
		userName = user.getUserName();
		if (!userName.equalsIgnoreCase("invalidUser") && !userId.equals(userName)){
         //		System.out.println("branchId======================="+((ChannelContext)request.getAttribute(IChannel.CHANNEL_CONTEXT)).getValueAt("branchId"));
			flag="2";//授权成功
		} else {
			flag="1";//失败
		    errorMsg=authormsg001;
		}
	} else {
		flag="1";//失败
		errorMsg=authormsg002;	 
	}
	if(flag=="2"&&mode!="3"){
		String sysName=SsicManager.getInstance().getSsicClientKeyName();
		PublicKey publicKey1 = SsicManager.getInstance().getPublicKey();
		PrivateKey privateKey1 = SsicManager.getInstance().getPrivateKey();
		String ssicIP = SsicManager.getInstance().getSsicIp();
	
		UpdateTellerSiteClient UpTellerSitetemp = new UpdateTellerSiteClient();

		UpTellerSitetemp.setKey(publicKey1,privateKey1);
		UpTellerSitetemp.setOpMode(mode);//授权操作类型，0：删除授权（用于注销）；1：添加授权（用于注册）
		UpTellerSitetemp.setSiteNo(sysName);//该标识对应密钥名称，如：IUCA
		UpTellerSitetemp.setSSICUrl("http://"+ssicIP+"/servlet/com.icbc.ssic.base.UpdateTellerSite");
		UpTellerSitetemp.setTellerNo(userId);
		UpTellerSitetemp.setTimeOut(200);
		flagauth = UpTellerSitetemp.excuteAccredit();
		if (flagauth!=0){
			    errorMsg=errorMsg+authormsg003;
			    flag="1";
		}else{
			flag="2";
		}
	}
%>
<script language="javascript">
   var retObj = {};
   retObj.flag = "<%=flag%>";
   retObj.errorMsg ="<%=errorMsg%>";
   parent.ctpMSB.doHandler(retObj,true);   //调用原页面回调函数传值
</script>

